
import Vue from 'vue';
import axios from 'axios';
import Vuex from 'vuex';
import HomeMudule from '../store/HomeMudule.js';
import UserMudule from '../store/UserMudule.js';

Vue.use(Vuex);


var store = new Vuex.Store({
    state:{
        isLogin:false,
        userinfo:"",
        bigkind:[],
        pageIn:true,
        bigKindItem:{},
        itemskindAll:[],
        isrequest:false,
        skindItem:{},
        showCar:false,
        findchart:[],
        cartItemReg:[],
    },
    modules:{
        home:HomeMudule,
        user:UserMudule,
    },
    mutations:{
        setbigKind:function(state,value){
            state.bigkind = value;
        },
        // 具体的某一个大类
        setbigKindItem:function(state,value){
            state.bigKindItem = value;
        },
         // 某个小类的所有商品
        setItemskindAll:function(state,value){
            state.itemskindAll = value;
        },
        // 是否开始发请求
        setIsrequest:function(state,value){
            state.isrequest = value;
        },
        // 某件商品详情
        setSkindItem:function(state,value){
            state.skindItem = value;
        },
          // 控制商品规格是否弹出
        setShowCar:function(state,value){
            state.showCar = value;
        },
         // 用户购物车商品
        setFindchart:function(state,value){
            state.findchart = value;
        },
        // 用户购物车编辑某件商品传规格
        setCartItemReg:function(state,value){
            state.cartItemReg = value;
        },
    },
    actions:{
        // 判断是否登录
        requestIsLogin(state){
            axios.get("/api/users/isLogin")
            .then(res=>{
                if(res.data.param){
                     state.state.isLogin=res.data.param.isLogin;
                     state.state.userinfo = res.data.param.userinfo;
                }else{
                     state.state.isLogin = res.data.isLogin;
                }
            });
        },
         // 请求大类列表和默认显示第一个
        requestBigKind:function(context,data){
            axios.get("/api/cates/bigkind").then(res=>{
                context.commit("setbigKind",res.data.data);
                var param = {
                    bigkind:res.data.data[0].bigkind
                }
                // console.dir(this);
                this.dispatch("requestBigKindItem",param);
                });
        },
        requestBigKindItem:function(context,pramas){
            axios.post("/api/cates/bigkinditem",pramas).then(res=>{  
                context.commit("setbigKindItem",res.data.data);
            });
        },
        // 请求某个小类的所有商品
        requestItemskindAll:function(context,params){
            console.log(params);
            return new Promise(function(resolve,reject){
                axios.get("/api/cates/itemskindAll",{params}).then(res=>{ 
                    resolve(res);
                    context.commit("setIsrequest",false);
                    
                    context.commit("setItemskindAll",res.data.data);
                })
            })
        },
        // 商品详情页面
        requestSkindItem:function(context,params){
            return new Promise(function(resolve,reject){
                axios.post("/api/cates/skindItem",params)
                .then(res=>{
                    resolve(res);
                });
            })
        },
         // 用户加入购物车请求
        requestChartAdd:function(context,params){
            return new Promise(function(resolve,reject){
                axios.post("/api/carts/chartadd",params)
                .then(res=>{
                    resolve(res);
                });
            })
        },
        // 用户购物车数据
        requestFindchart:function(context,params){
            return new Promise(function(resolve,reject){
                axios.post("/api/carts/findchart",params)
                .then(res=>{
                    resolve(res);
                });
            })
        },
        // 用户购物车修改商品数量和商品规格
        requestEditChart:function(context,params){
            return new Promise(function(resolve,reject){
                axios.post("/api/carts/editchart",params)
                .then(res=>{
                    resolve(res);
                });
            })
        },
        // 用户删除购物车商品
        requestDeleteChart:function(context,params){
            return new Promise(function(resolve,reject){
                axios.post("/api/carts/deletechart",params)
                .then(res=>{
                    resolve(res);
                });
            })
        },
    },
        
});

export default store;